﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace YXShop.Web.admin.stocks
{
    public partial class barcode_create : System.Web.UI.Page
    {
        YXShop.BLL.Product.ProductInfo productBll = new BLL.Product.ProductInfo();
        YXShop.BLL.Stocks.BarCode barcodeBll = new BLL.Stocks.BarCode();
        YXShop.BLL.Stocks.OutProduct outproductBll = new BLL.Stocks.OutProduct();
        YXShop.BLL.Stocks.ProductTruck bllProductTruck = new BLL.Stocks.ProductTruck();
        private List<int> ids = new List<int>();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //YXShop.Common.PromptInfo.Popedom("014001001");
                YXShop.Model.Admin.AdminInfo admin = YXShop.Common.AdministrorManager.Get();
                if (admin == null)
                {
                    this.ltlMsg.Text = "用户操作失败，请重新登录";
                    this.pnlMsg.Visible = true;
                    this.pnlMsg.CssClass = "actionErr";
                    return;
                }
                else
                {
                    if (admin.AdminType != 1)
                    {
                        this.ltlMsg.Text = "用户权限不足！";
                        this.pnlMsg.Visible = true;
                        this.pnlMsg.CssClass = "actionErr";
                        return;
                    }
                    else
                    {
                        string productids = ChangeHope.WebPage.PageRequest.GetQueryString("barchId");
                        this.hfids.Value = productids;
                        if (productids != "")
                        {
                            string[] arrids = productids.Split(',');
                            foreach (string s in arrids)
                            {
                                ids.Add(int.Parse(s));
                            }
                        }
                        this.lblList.Text = GetList(ids);
                    }
                }
            }
        }

        private string GetList(List<int> productids)
        {
            ChangeHope.WebPage.Table table = new ChangeHope.WebPage.Table();
            table.AddHeadCol("", "商品名称");
            table.AddHeadCol("", "待生成条码数");
            table.AddRow();
            ChangeHope.DataBase.DataByPage dataPage = new ChangeHope.DataBase.DataByPage();
            dataPage.RecordCount = productids.Count;
            if (productids.Count > 0)
            {
                int curpage = ChangeHope.WebPage.PageRequest.GetInt("pageindex");
                if (curpage < 0)
                {
                    curpage = 1;
                }
                for (int i = 0; i < productids.Count; i++)
                {
                    string No = (15 * (curpage - 1) + (i + 1)).ToString();
                    table.AddCol(productBll.GetModel(productids[i]).pro_Name);
                    table.AddCol(GetCountByID(productids[i]).ToString());
                    table.AddRow();
                }
            }
            string view = table.GetTable();
            dataPage.Dispose();
            dataPage = null;
            return view;
        }

        #region  根据商品ID 获取需要生成条码的数量数据
        private int GetCountByID(int id)
        {
            int count = 0;
            int countall = 0;
           Model.Product.ProductInfo productModel= productBll.GetModel(id);
           if (productModel.pro_PutoutType == 2)
           {
               if (productModel.pro_Stock!= null)
               {
                   countall = (int)productModel.pro_Stock;
               }
           }
           if (countall != 0)
           {
               List<Model.Stocks.BarCode> list = barcodeBll.GetListBarCodeByWhere("productid=" + id + " and stocktypeid =" + (int)Model.Stocks.StockTypeEnum.CenterStock + " and (stateid=0 or stateid=1 or stateid=4)");
               count = countall - list.Count;
           }
            return count;
        }
        #endregion
        private int CreateBarCode()
        {
            
            int i = 0;
            try
            {
                Model.Stocks.BarCode barcodeModel;
                Model.Stocks.OutProduct outproductModel;
                Model.Stocks.ProductTruck productTruckModel;
                 YXShop.Model.Admin.AdminInfo admin = YXShop.Common.AdministrorManager.Get();
                if(admin!=null)
                {
                string[] arrids = this.hfids.Value.Split(',');
                if (arrids.Length > 0)
                {
                    foreach (string s in arrids)
                    {
                        if (s != "")
                        {
                            int id = int.Parse(s);
                            int barcodes = GetCountByID(id);
                            if (barcodes > 0)
                            {
                                for (int j = 0; j < barcodes; j++)
                                {
                                    i++;
                                    barcodeModel = new Model.Stocks.BarCode();
                                    barcodeModel.Barcode = Guid.NewGuid().ToString();
                                    barcodeModel.InTime = DateTime.Now;
                                    barcodeModel.OutTime = DateTime.MaxValue;
                                    barcodeModel.Productid = id;
                                    barcodeModel.StateID = (int)Model.Stocks.ProductStateEnum.Order;
                                    barcodeModel.StockTypeID = (int)Model.Stocks.StockTypeEnum.CenterStock;
                                    barcodeModel.StockID = 0;//中央仓库 id 0；
                                    barcodeModel.Notes = "";
                                    outproductModel = new Model.Stocks.OutProduct();
                                    outproductModel.Intype = (int)Model.Stocks.InTypeEnum.Normal;
                                    outproductModel.StockTypeID = (int)Model.Stocks.StockTypeEnum.CenterStock;
                                    outproductModel.StockID = 0;//中央仓库id为0  0823修改
                                    outproductModel.BarcodeID= barcodeBll.Add(barcodeModel);
                                    outproductBll.Add(outproductModel);
                                    productTruckModel = new Model.Stocks.ProductTruck();
                                    productTruckModel.OperationTime = DateTime.Now;
                                    productTruckModel.BarcodeID = outproductModel.BarcodeID;
                                    productTruckModel.OperationType = (int)Model.Stocks.ProductStateEnum.Order;
                                    productTruckModel.StoreType = (int)Model.Stocks.StockTypeEnum.CenterStock;
                                    productTruckModel.StoreID = 0;
                                    productTruckModel.Adminid = admin.AdminId;
                                    bllProductTruck.Add(productTruckModel);
                                }
                            }
                        }
                    }
                }
               
                this.ltlMsg.Text = "操作成功，生成条码共：" + i.ToString() + "条";
                this.pnlMsg.Visible = true;
                this.pnlMsg.CssClass = "actionOk";
                }
                
            }
            catch (Exception ex)
            {
                this.ltlMsg.Text = "操作失败：\n" + ex.Message;
                this.pnlMsg.Visible = true;
                this.pnlMsg.CssClass = "actionErr";
            }
            finally
            {
                //刷新页面
                Response.AddHeader("Refresh", "0");
            }
            return i;
        }

        protected void btnCreateBarCode_Click(object sender, EventArgs e)
        {
            CreateBarCode();
        }
    }

}